﻿/*
 * Created by SharpDevelop.
 * User: christophe
 * Date: 15/03/2011
 * Time: 20:38
 * 
 * To change this template use Tools | Options | Coding | Edit Standard Headers.
 */
using System;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.IO;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Data;
using System.Windows.Input;

using GalaSoft.MvvmLight;
using GalaSoft.MvvmLight.Command;
using MetaVideoEditorEvolved.Entity;
using MetaVideoEditorEvolved.UI;
using mveEngine;

namespace MetaVideoEditorEvolved.ViewModel
{
	/// <summary>
	/// Description of MainWindowVM.
	/// </summary>
	public class MainWindowVM : ViewModelBase
	{
		MetaVideoEditorEvolved.UI.Entity.KernelEntity uiKernel;
		
		private RelayCommand loadedCommand;

		private UI.Entity.ActorMediaItem selectedActor;

		private RelayCommand clearMetadataCommand;

		private RelayCommand removeSelectedActorCommand;

		private RelayCommand addPosterCommand;

		private ObservableCollection<MediaItem> mediaItems = new ObservableCollection<MediaItem>();
		
		private ObservableCollection<MveAction> actions = new ObservableCollection<MveAction>();

		private MveAction currentAction;

		private RelayCommand autoSearchCommand;

		private RelayCommand saveCurrentCommand;

		private RelayCommand rebuildLibraryCommand;

		private RelayCommand manualSearchCommand;

		private RelayCommand saveAllCommand;

		private RelayCommand aboutCommand;

		private RelayCommand downloadTrailer;
		
		private RelayCommand<string> filterCommand;

		private RelayCommand<string> downloadTrailerAndConvertCommand;

		private RelayCommand playTrailerCommand;
		
		private RelayCommand editGenreMappingTableCommand;
		
		private RelayCommand clearSelectionCommand;

		private RelayCommand editMainDataCommand,
		deselectAllBackdrops;

		private UI.Entity.TrailerMediaItem selectedTrailer;

		private CollectionViewSource availableTrailers,
		downloadedTrailers,
		imagesPathHttp,
		backdropsHttp,
		backdropsLocal,
		imagesPathLocal,
		bannerPathHttp,
		bannerPathLocal;
		
		private bool isEditAvailable;
		
		private MediaFilter filter;

		private Helper.Dispatcher dispatcher = new MetaVideoEditorEvolved.Helper.Dispatcher();

		private int thumbnailsSize = 100;
		
		private bool isFilterMovie;
		
		private bool isFilterSerie;
		
		private bool isFilterDataStateNotFound;
		
		private bool isFilterDataStateMiddle;
		
		private bool isFilterDataStateFound;
		
		private string filterTitleSearch;
		
		public MetaVideoEditorEvolved.UI.Entity.KernelEntity UiKernel {
			get
			{
				return this.uiKernel;
			}
			
			set
			{
				this.uiKernel = value;
				this.RaisePropertyChanged("UiKernel");
			}
		}
		
		public RelayCommand EditGenreMappingTableCommand
		{
			get
			{
				return editGenreMappingTableCommand;
			}
			
			set
			{
				editGenreMappingTableCommand = value;
				RaisePropertyChanged("EditGenreMappingTableCommand");
			}
		}
		
		public RelayCommand ClearSelectionCommand
		{
			get
			{
				return clearSelectionCommand;
			}
			
			set
			{
				clearSelectionCommand = value;
				RaisePropertyChanged("ClearSelectionCommand");
			}
		}
		
		public int ThumbnailsSize
		{
			get
			{
				return thumbnailsSize;
			}
			
			set
			{
				thumbnailsSize = value;
				RaisePropertyChanged("ThumbnailsSize");
			}
		}
		
		public string FilterTitleSearch
		{
			get
			{
				return filterTitleSearch;
			}
			
			set
			{
				filterTitleSearch = value;
				RaisePropertyChanged("FilterTitleSearch");
				
				this.filterCommand.Execute(null);
			}
		}
		
		public MediaFilter Filter
		{
			get
			{
				return filter;
			}
			
			set
			{
				filter = value;
				RaisePropertyChanged("Filter");
			}
		}
		
		public bool IsFilterDataStateNotFound
		{
			get
			{
				return isFilterDataStateNotFound;
			}

			set
			{
				isFilterDataStateNotFound = value;
				
				if (value) {
					this.IsFilterDataStateMiddle = false;
					this.IsFilterDataStateFound = false;
				}
				
				this.RaisePropertyChanged("IsFilterDataStateNotFound");
			}
		}
		
		public bool IsFilterDataStateMiddle
		{
			get
			{
				return isFilterDataStateMiddle;
			}

			set
			{
				isFilterDataStateMiddle = value;
				
				if (value) {
					this.IsFilterDataStateNotFound = false;
					this.IsFilterDataStateFound = false;
				}
				
				this.RaisePropertyChanged("IsFilterDataStateMiddle");
			}
		}
		
		public bool IsFilterDataStateFound
		{
			get
			{
				return isFilterDataStateFound;
			}

			set
			{
				isFilterDataStateFound = value;
				
				if (value) {
					this.IsFilterDataStateNotFound = false;
					this.IsFilterDataStateMiddle = false;
				}
				
				this.RaisePropertyChanged("IsFilterDataStateFound");
			}
		}
		
		public bool IsEditAvailable
		{
			get
			{
				return isEditAvailable;
			}

			set
			{
				isEditAvailable = value;
				this.RaisePropertyChanged("IsEditAvailable");
			}
		}
		
		public bool IsFilterMovie
		{
			get
			{
				return this.isFilterMovie;
			}

			set
			{
				isFilterMovie = value;
				
				if (isFilterMovie) {
					this.IsFilterSerie = false;
				}
				
				this.RaisePropertyChanged("IsFilterMovie");
			}
		}
		
		public bool IsFilterSerie
		{
			get
			{
				return this.isFilterSerie;
			}

			set
			{
				isFilterSerie = value;
				
				if (isFilterSerie) {
					this.IsFilterMovie = false;
				}
				
				this.RaisePropertyChanged("IsFilterSerie");
			}
		}

		public UI.Entity.ActorMediaItem SelectedActor
		{
			get
			{
				return selectedActor;
			}

			set
			{
				selectedActor = value;
				this.RaisePropertyChanged("SelectedActor");
			}
		}

		public CollectionViewSource DownloadedTrailers
		{
			get
			{
				return downloadedTrailers;
			}

			set
			{
				downloadedTrailers = value;
				this.RaisePropertyChanged("DownloadedTrailers");
			}
		}

		public CollectionViewSource AvailableTrailers
		{
			get
			{
				return availableTrailers;
			}

			set
			{
				availableTrailers = value;
				this.RaisePropertyChanged("AvailableTrailers");
			}
		}
		
		public CollectionViewSource BannerPathHttp
		{
			get
			{
				return bannerPathHttp;
			}

			set
			{
				bannerPathHttp = value;
				this.RaisePropertyChanged("BannerPathHttp");
			}
		}
		
		public CollectionViewSource BannerPathLocal
		{
			get
			{
				return bannerPathLocal;
			}

			set
			{
				bannerPathLocal = value;
				this.RaisePropertyChanged("BannerPathLocal");
			}
		}
		
		public CollectionViewSource ImagesPathHttp
		{
			get
			{
				return imagesPathHttp;
			}

			set
			{
				imagesPathHttp = value;
				this.RaisePropertyChanged("ImagesPathHttp");
			}
		}
		
		public CollectionViewSource ImagesPathLocal
		{
			get
			{
				return imagesPathLocal;
			}

			set
			{
				imagesPathLocal = value;
				this.RaisePropertyChanged("ImagesPathLocal");
			}
		}
		
		public CollectionViewSource BackdropsHttp
		{
			get
			{
				return backdropsHttp;
			}

			set
			{
				backdropsHttp = value;
				this.RaisePropertyChanged("BackdropsHttp");
			}
		}
		
		public CollectionViewSource BackdropsLocal
		{
			get
			{
				return backdropsLocal;
			}

			set
			{
				backdropsLocal = value;
				this.RaisePropertyChanged("BackdropsLocal");
			}
		}

		public RelayCommand<string> DownloadTrailerAndConvertCommand
		{
			get
			{
				return downloadTrailerAndConvertCommand;
			}

			set
			{
				downloadTrailerAndConvertCommand = value;
				this.RaisePropertyChanged("DownloadTrailerAndConvertCommand");
			}
		}

		public RelayCommand ClearMetadataCommand
		{
			get
			{
				return clearMetadataCommand;
			}

			set
			{
				clearMetadataCommand = value;
				this.RaisePropertyChanged("ClearMetadataCommand");
			}
		}

		public RelayCommand RemoveSelectedActorCommand
		{
			get
			{
				return removeSelectedActorCommand;
			}

			set
			{
				removeSelectedActorCommand = value;
				this.RaisePropertyChanged("RemoveSelectedActorCommand");
			}
		}

		public RelayCommand AddPosterCommand
		{
			get
			{
				return addPosterCommand;
			}

			set
			{
				addPosterCommand = value;
				this.RaisePropertyChanged("AddPosterCommand");
			}
		}

		public RelayCommand AboutCommand
		{
			get
			{
				return aboutCommand;
			}

			set
			{
				aboutCommand = value;
				this.RaisePropertyChanged("AboutCommand");
			}
		}

		public RelayCommand EditMainDataCommand
		{
			get
			{
				return editMainDataCommand;
			}

			set
			{
				editMainDataCommand = value;
				this.RaisePropertyChanged("EditMainDataCommand");
			}
		}

		public RelayCommand PlayTrailerCommand
		{
			get
			{
				return playTrailerCommand;
			}

			set
			{
				playTrailerCommand = value;
				this.RaisePropertyChanged("PlayTrailerCommand");
			}
		}
		
		public RelayCommand<string> FilterCommand
		{
			get
			{
				return this.filterCommand;
			}

			set
			{
				this.filterCommand = value;
				this.RaisePropertyChanged("FilterCommand");
			}
		}

		public RelayCommand DownloadTrailer
		{
			get
			{
				return downloadTrailer;
			}

			set
			{
				downloadTrailer = value;
				this.RaisePropertyChanged("DownloadTrailer");
			}
		}
		
		public RelayCommand DeselectAllBackdrops
		{
			get
			{
				return deselectAllBackdrops;
			}

			set
			{
				deselectAllBackdrops = value;
				this.RaisePropertyChanged("DeselectAllBackdrops");
			}
		}

		public RelayCommand ManualSearchCommand
		{
			get
			{
				return this.manualSearchCommand;
			}

			set
			{
				this.manualSearchCommand = value;
				this.RaisePropertyChanged("ManualSearchCommand");
			}
		}

		public RelayCommand SaveAllCommand
		{
			get
			{
				return this.saveAllCommand;
			}

			set
			{
				this.saveAllCommand = value;
				this.RaisePropertyChanged("SaveAllCommand");
			}
		}

		public RelayCommand RebuildLibraryCommand
		{
			get
			{
				return this.rebuildLibraryCommand;
			}

			set
			{
				this.rebuildLibraryCommand = value;
				this.RaisePropertyChanged("RebuildLibraryCommand");
			}
		}

		public RelayCommand SaveCurrentCommand
		{
			get
			{
				return this.saveCurrentCommand;
			}

			set
			{
				this.saveCurrentCommand = value;
				this.RaisePropertyChanged("SaveCurrentCommand");
			}
		}

		public RelayCommand AutoSearchCommand
		{
			get
			{
				return this.autoSearchCommand;
			}

			set
			{
				this.autoSearchCommand = value;
				this.RaisePropertyChanged("AutoSearchCommand");
			}
		}

		public MveAction CurrentAction
		{
			get
			{
				return this.currentAction;
			}

			set
			{
				this.currentAction = value;
				this.RaisePropertyChanged("CurrentAction");
			}
		}

		public ObservableCollection<MediaItem> MediaItems
		{
			get
			{
				return this.mediaItems;
			}
		}

		MetaVideoEditorEvolved.UI.Entity.MediaItemBaseEntity selectedUiMediaItem;
		
		public MetaVideoEditorEvolved.UI.Entity.MediaItemBaseEntity SelectedUiMediaItem {
			get
			{
				return this.selectedUiMediaItem;
			}
			
			set
			{
				this.selectedUiMediaItem = value;
				
				var video = this.SelectedUiMediaItem as MetaVideoEditorEvolved.UI.Entity.VideoMediaElement;
				if(value!=null)
				{
					if(video!=null)
					{
						this.ImagesPathHttp.Source = video.Posters;
						this.ImagesPathHttp.View.Refresh();
						
						this.ImagesPathLocal.Source = video.Posters;
						this.ImagesPathLocal.View.Refresh();
						
						this.BackdropsHttp.Source =  video.Backdrops;
						this.BackdropsHttp.View.Refresh();
						
						this.BackdropsLocal.Source = video.Backdrops;
						this.BackdropsLocal.View.Refresh();
						
						this.AvailableTrailers.Source = video.Trailers;
						this.AvailableTrailers.View.Refresh();

						this.DownloadedTrailers.Source = video.Trailers;
						this.DownloadedTrailers.View.Refresh();
						
						var serie = video as UI.Entity.SerieMediaItem;
						if(serie!=null)
						{
							this.BannerPathHttp.Source = serie.Banners;
							this.BannerPathHttp.View.Refresh();
							
							this.BannerPathLocal.Source = serie.Banners;
							this.BannerPathLocal.View.Refresh();
						}
						else
						{
							this.BannerPathHttp.Source = null;
							this.BannerPathLocal.Source = null;
						}
					}
					else
					{
						this.ImagesPathHttp.Source = null;
						this.ImagesPathLocal.Source = null;
						this.BackdropsHttp.Source =  null;
						this.BackdropsLocal.Source = null;
						this.AvailableTrailers.Source = null;
						this.DownloadedTrailers.Source = null;
						this.BannerPathHttp.Source = null;
						this.BannerPathLocal.Source = null;
					}
					
					this.IsEditAvailable = video!=null;
					
					SelectedPoster = null;
				}
				else
				{
					this.IsEditAvailable = false;
				}
				
				this.RaisePropertyChanged("SelectedUiMediaItem");
			}
		}
		
		public ObservableCollection<MveAction> Actions
		{
			get
			{
				return this.actions;
			}
		}

		public RelayCommand LoadedCommand
		{
			get
			{
				return this.loadedCommand;
			}

			set
			{
				this.loadedCommand = value;
				this.RaisePropertyChanged("LoadedCommand");
			}
		}

		public ICommand CreateMediaFolderCommand { get; private set; }
		public ICommand OpenCurrentCommand { get; private set; }
		public ICommand DeleteCurrentCommand { get; private set; }
		public ICommand SettingsCommand { get; private set; }
		public ICommand DefaultImageCommand { get; private set; }
		
		private int selectedTabIndex;
		
		public int SelectedTabIndex
		{
			get
			{
				return selectedTabIndex;
			}
			
			set
			{
				selectedTabIndex = value;
				SelectedPoster = null;
				RaisePropertyChanged("SelectedTabIndex");
			}
		}
		
		private UI.Entity.PosterMediaItem selectedPoster;
		
		public UI.Entity.PosterMediaItem SelectedPoster
		{
			get
			{
				return selectedPoster;
			}
			
			set
			{
				selectedPoster = value;
				RaisePropertyChanged("SelectedPoster");
			}
		}
		
		
		public UI.Entity.TrailerMediaItem SelectedTrailer
		{
			get { return selectedTrailer; }
			set
			{
				selectedTrailer = value;
				RaisePropertyChanged("SelectedTrailer");
			}
		}

		public MainWindowVM()
		{
			this.uiKernel = MetaVideoEditorEvolved.UI.KernelSessionManager.SessionInstance.KernelUiEntity;
			
			this.editGenreMappingTableCommand = new RelayCommand(()=>
			                                                     {
			                                                     	GalaSoft.MvvmLight.Messaging.Messenger.Default.Send(new Messages.OpenWindowMessage() {WindowToOpen = Messages.WindowType.GenreMappingWindow});
			                                                     });
			
			this.DefaultImageCommand = new RelayCommand(() =>
			                                            {
			                                            	var video = this.SelectedUiMediaItem as UI.Entity.VideoMediaElement;
			                                            	var mveItem = KernelSessionManager.SessionInstance.RealKernel.ItemCollection.ItemsList.First(i=>i.Id == video.Id.ToString());
			                                            	
			                                            	if(video is UI.Entity.MovieMediaItem || video is UI.Entity.FolderMediaItem)
			                                            	{
			                                            		if(SelectedTabIndex == 3)
			                                            		{
			                                            			// unselect previous poster
			                                            			if (video.PrimaryPoster!=null) {
			                                            				video.PrimaryPoster.Checked = false;
			                                            				mveItem.PrimaryImage.Checked = false;
			                                            				video.Posters.First(p=>p.Image == video.PrimaryPoster.Image).Checked = false;
			                                            			}
			                                            			
			                                            			video.PrimaryPoster = this.SelectedPoster;
			                                            			video.PrimaryPoster.Checked=true;
			                                            			mveItem.PrimaryImage = mveItem.ImagesPaths.First(p=>p.Image == video.PrimaryPoster.Image);
			                                            			mveItem.PrimaryImage.Checked = true;
			                                            		}
			                                            		
			                                            		if(SelectedTabIndex == 4)
			                                            		{
			                                            			// unselect previous backdrop
			                                            			if (video.PrimaryBackdrop!=null) {
			                                            				video.PrimaryBackdrop.Checked = false;
			                                            				mveItem.PrimaryBackdrop.Checked = false;
			                                            				video.Backdrops.First(p=>p.Image == video.PrimaryBackdrop.Image).Checked = false;
			                                            			}
			                                            			
			                                            			video.PrimaryBackdrop = SelectedPoster;
			                                            			video.PrimaryBackdrop.Checked=true;
			                                            			mveItem.PrimaryBackdrop = mveItem.BackdropImagePaths.First(p=>p.Image == video.PrimaryBackdrop.Image);
			                                            			mveItem.PrimaryBackdrop.Checked = true;
			                                            		}
			                                            		
			                                            		
			                                            	}
			                                            	
			                                            	if(video is UI.Entity.SerieMediaItem)
			                                            	{
			                                            		if(SelectedTabIndex == 2)
			                                            		{
			                                            			// unselect previous poster
			                                            			if (video.PrimaryPoster!=null) {
			                                            				video.PrimaryPoster.Checked = false;
			                                            				mveItem.PrimaryImage.Checked = false;
			                                            				video.Posters.First(p=>p.Image == video.PrimaryPoster.Image).Checked = false;
			                                            			}
			                                            			
			                                            			video.PrimaryPoster = this.SelectedPoster;
			                                            			video.PrimaryPoster.Checked=true;
			                                            			mveItem.PrimaryImage = mveItem.ImagesPaths.First(p=>p.Image == video.PrimaryPoster.Image);
			                                            			mveItem.PrimaryImage.Checked = true;
			                                            		}
			                                            		
			                                            		if(SelectedTabIndex == 3)
			                                            		{
			                                            			// unselect previous backdrop
			                                            			if (video.PrimaryBackdrop!=null) {
			                                            				video.PrimaryBackdrop.Checked = false;
			                                            				mveItem.PrimaryBackdrop.Checked = false;
			                                            				video.Backdrops.First(p=>p.Image == video.PrimaryBackdrop.Image).Checked = false;
			                                            			}
			                                            			
			                                            			video.PrimaryBackdrop = SelectedPoster;
			                                            			video.PrimaryBackdrop.Checked=true;
			                                            			mveItem.PrimaryBackdrop = mveItem.BackdropImagePaths.First(p=>p.Image == video.PrimaryBackdrop.Image);
			                                            			mveItem.PrimaryBackdrop.Checked = true;
			                                            		}
			                                            		
			                                            		if(SelectedTabIndex == 4)
			                                            		{
			                                            			var serie = video as UI.Entity.SerieMediaItem;
			                                            			// unselect previous banners
			                                            			if (serie.PrimaryBanner!=null) {
			                                            				serie.PrimaryBanner.Checked = false;
			                                            				mveItem.PrimaryBanner.Checked = false;
			                                            				serie.Banners.First(p=>p.Image == serie.PrimaryBanner.Image).Checked = false;
			                                            			}
			                                            			
			                                            			serie.PrimaryBanner = SelectedPoster;
			                                            			serie.PrimaryBanner.Checked=true;
			                                            			mveItem.PrimaryBanner = mveItem.BannersPaths.First(p=>p.Image == serie.PrimaryBanner.Image);
			                                            			mveItem.PrimaryBanner.Checked = true;
			                                            		}
			                                            	}
			                                            	
			                                            	if(SelectedUiMediaItem is UI.Entity.SeasonMediaItem)
			                                            	{
			                                            		if(SelectedTabIndex == 1)
			                                            		{
			                                            			// unselect previous poster
			                                            			if (video.PrimaryPoster!=null) {
			                                            				video.PrimaryPoster.Checked = false;
			                                            				mveItem.PrimaryImage.Checked = false;
			                                            				video.Posters.First(p=>p.Image == video.PrimaryPoster.Image).Checked = false;
			                                            			}
			                                            			
			                                            			video.PrimaryPoster = this.SelectedPoster;
			                                            			video.PrimaryPoster.Checked=true;
			                                            			mveItem.PrimaryImage = mveItem.ImagesPaths.First(p=>p.Image == video.PrimaryPoster.Image);
			                                            			mveItem.PrimaryImage.Checked = true;
			                                            		}
			                                            	}
			                                            	
			                                            	this.SelectedUiMediaItem.IsCheckedForUpdate = true;
			                                            	
			                                            },
			                                            () =>
			                                            {
			                                            	if(SelectedUiMediaItem is UI.Entity.FolderMediaItem ||
			                                            	   SelectedUiMediaItem is UI.Entity.MovieMediaItem)
			                                            	{
			                                            		return ((SelectedTabIndex == 3 ||
			                                            		         SelectedTabIndex == 4) ||
			                                            		        SelectedTabIndex == 5) &&
			                                            			(this.SelectedPoster != null &&
			                                            			 this .SelectedUiMediaItem as UI.Entity.VideoMediaElement  != null);
			                                            	}
			                                            	
			                                            	if(SelectedUiMediaItem is UI.Entity.SerieMediaItem)
			                                            	{
			                                            		return ((SelectedTabIndex == 2 ||
			                                            		         SelectedTabIndex == 3) ||
			                                            		        SelectedTabIndex == 4) &&
			                                            			(this.SelectedPoster != null &&
			                                            			 this .SelectedUiMediaItem as UI.Entity.VideoMediaElement  != null);
			                                            	}
			                                            	
			                                            	if(SelectedUiMediaItem is UI.Entity.SeasonMediaItem)
			                                            	{
			                                            		return SelectedTabIndex == 1 &&
			                                            			this.SelectedPoster != null &&
			                                            			this .SelectedUiMediaItem as UI.Entity.VideoMediaElement  != null;
			                                            	}
			                                            	
			                                            	return false;
			                                            });
			
			
			this.SettingsCommand = new RelayCommand(() =>
			                                        {
			                                        	var settingsContext = new SettingsWindowVM();
			                                        	
			                                        	new SettingsWindow()
			                                        	{
			                                        		DataContext = settingsContext
			                                        	}.ShowDialog();
			                                        	
			                                        	if(settingsContext.CanRebuild)
			                                        	{
			                                        		RebuildLibrary(true, settingsContext.CanRebuild);
			                                        	}
			                                        });
			
			this.OpenCurrentCommand = new RelayCommand(() =>
			                                           {
			                                           	var file = this.SelectedUiMediaItem as UI.Entity.FileMediaElement;
			                                           	if (Directory.Exists(file.Path))
			                                           	{
			                                           		System.Diagnostics.Process.Start(file.Path);
			                                           	}
			                                           	else if (File.Exists(file.Path))
			                                           	{
			                                           		System.Diagnostics.Process.Start(Directory.GetParent(file.Path).FullName);
			                                           	}
			                                           },
			                                           () =>
			                                           {
			                                           	var file = this.SelectedUiMediaItem as UI.Entity.FileMediaElement;
			                                           	return  file!= null;
			                                           });

			this.DeleteCurrentCommand = new RelayCommand(() =>
			                                             {
			                                             	try
			                                             	{var file = this.SelectedUiMediaItem as UI.Entity.FileMediaElement;
			                                             		
			                                             		if (Directory.Exists(file.Path))
			                                             		{
			                                             			if (this.ShowQuestion(string.Format("{0} : '{1}' ?", this.CreateKernelHelper().GetString("DeleteDirectoryMessage"), file.Name)) == MessageBoxResult.Yes)
			                                             			{
			                                             				Directory.Delete(file.Path, true);
			                                             				this.RemoveSelectedItem();
			                                             			}
			                                             		}
			                                             		else if (File.Exists(file.Path))
			                                             		{
			                                             			if (this.ShowQuestion(string.Format("{0} : '{1}' ?", this.CreateKernelHelper().GetString("DeleteFileMessage"), file.Name)) == MessageBoxResult.Yes)
			                                             			{
			                                             				File.Delete(file.Path);
			                                             				this.RemoveSelectedItem();
			                                             			}
			                                             		}
			                                             	}
			                                             	catch(Exception ex)
			                                             	{
			                                             		this.ShowErrorMessage("Impossible de supprimer le media, vérifier que celui-ci n'est pas en lecture seule");
			                                             		MetaVideoEditorEvolved.Helper.Tracer.TracerInstance.TraceError("Error while removing media from tree", ex);
			                                             	}
			                                             }, () =>
			                                             {
			                                             	var file = this.SelectedUiMediaItem as UI.Entity.FileMediaElement;
			                                             	return file!=null;
			                                             });

			this.autoSearchCommand = new RelayCommand(() =>
			                                          {
			                                          	this.AutoSearchClicked();
			                                          }, () =>
			                                          {
			                                          	return (from m in this.UiKernel.FlatListOfMediaItems.ToList()
			                                          	        where m.IsCheckedForUpdate
			                                          	        select m).Any();
			                                          });

			this.saveCurrentCommand = new RelayCommand(() =>
			                                           {
			                                           	this.SaveSelectedItem();
			                                           },
			                                           () =>
			                                           {
			                                           	return this.SelectedUiMediaItem != null && this.SelectedUiMediaItem is UI.Entity.VideoMediaElement;
			                                           });

			this.rebuildLibraryCommand = new RelayCommand(() =>
			                                              {
			                                              	this.RebuildLibrary(false, false);
			                                              },
			                                              () =>
			                                              {
			                                              	return true;
			                                              });

			this.manualSearchCommand = new RelayCommand(() =>
			                                            {
			                                            	new Search()
			                                            	{
			                                            		DataContext = new SearchVM(this.SelectedUiMediaItem as MetaVideoEditorEvolved.UI.Entity.VideoMediaElement)
			                                            	}.ShowDialog();
			                                            	
			                                            	var videoUpdated = this.SelectedUiMediaItem as UI.Entity.VideoMediaElement;
			                                            	if(videoUpdated!=null && videoUpdated.HasChanged && videoUpdated.Backdrops.Any())
			                                            	{
			                                            		this.BackdropsHttp.Source = videoUpdated.Backdrops;
			                                            		this.backdropsHttp.View.Refresh();
			                                            		
			                                            		this.BackdropsLocal.Source = videoUpdated.Backdrops;
			                                            		this.BackdropsLocal.View.Refresh();
			                                            	}
			                                            },
			                                            () =>
			                                            {
			                                            	return this.SelectedUiMediaItem != null &&
			                                            		this.SelectedUiMediaItem as MetaVideoEditorEvolved.UI.Entity.VideoMediaElement != null &&
			                                            		!(this.SelectedUiMediaItem is MetaVideoEditorEvolved.UI.Entity.EpisodeMediaItem) &&
			                                            		!(this.SelectedUiMediaItem is MetaVideoEditorEvolved.UI.Entity.SeasonMediaItem);
			                                            });

			this.loadedCommand = new RelayCommand(new Action(() =>
			                                                 {
			                                                 	this.Loaded();
			                                                 }));

			this.SaveAllCommand = new RelayCommand(new Action(() =>
			                                                  {
			                                                  	this.SaveAll();
			                                                  }),
			                                       () =>
			                                       {
			                                       	var query = from media in this.UiKernel.FlatListOfMediaItems
			                                       		where media is UI.Entity.FileMediaElement &&
			                                       		((UI.Entity.FileMediaElement)media).HasChanged
			                                       		select media;

			                                       	return query.Any();
			                                       });
			this.CreateMediaFolderCommand = new RelayCommand(new Action(() =>
			                                                            {
			                                                            	new CreateMediaFolderWindow()
			                                                            	{
			                                                            		DataContext = new CreateMediaFolderVM()
			                                                            	}.ShowDialog();
			                                                            	
			                                                            	this.RebuildLibrary(false, false);
			                                                            }));

			this.aboutCommand = new RelayCommand(() =>
			                                     {
			                                     	var aboutWm = new AboutWindow();
			                                     	aboutWm.DataContext = new AboutWindowVM();

			                                     	aboutWm.ShowDialog();
			                                     });

			this.downloadTrailer = new RelayCommand(new Action(() =>
			                                                   {
			                                                   	string downloadedTrailer = string.Empty;
			                                                   	Action downloader = new Action(() =>
			                                                   	                               {
			                                                   	                               	downloadedTrailer = this.CreateKernelHelper().DownloadTrailer(this.SelectedTrailer, (UI.Entity.VideoMediaElement) this.SelectedUiMediaItem);
			                                                   	                               });
			                                                   	var waitWindow = new WaitingWindow(downloader, this.CreateKernelHelper().GetString(""));
			                                                   	waitWindow.ShowDialog();

			                                                   	this.SelectedTrailer.FilePath = downloadedTrailer;
			                                                   	((UI.Entity.VideoMediaElement) this.SelectedUiMediaItem).IsCheckedForUpdate = true;
			                                                   	this.DownloadedTrailers.View.Refresh();
			                                                   	this.AvailableTrailers.View.Refresh();
			                                                   }),
			                                        () =>
			                                        {
			                                        	if (this.SelectedTrailer == null || this.SelectedTrailer.FilePath.Contains("http://") == false)
			                                        	{
			                                        		return false;
			                                        	}
			                                        	else
			                                        	{
			                                        		return true;
			                                        	}
			                                        });

			this.downloadTrailerAndConvertCommand = new RelayCommand<string>(new Action<string>((param) =>
			                                                                                    {
			                                                                                    	string downloadedTrailer = string.Empty;
			                                                                                    	Action downloader = new Action(() =>
			                                                                                    	                               {
			                                                                                    	                               	downloadedTrailer = this.CreateKernelHelper().DownloadAndConvertTrailer(this.SelectedTrailer, (UI.Entity.VideoMediaElement) this.SelectedUiMediaItem, (VideoType)Enum.Parse(typeof(VideoType), param));
			                                                                                    	                               });
			                                                                                    	var waitWindow = new WaitingWindow(downloader, this.CreateKernelHelper().GetString(""));
			                                                                                    	waitWindow.ShowDialog();

			                                                                                    	this.SelectedTrailer.FilePath = downloadedTrailer;
			                                                                                    	((UI.Entity.VideoMediaElement) this.SelectedUiMediaItem).IsCheckedForUpdate = true;
			                                                                                    	this.DownloadedTrailers.View.Refresh();
			                                                                                    	this.AvailableTrailers.View.Refresh();
			                                                                                    }),
			                                                                 (param) =>
			                                                                 {
			                                                                 	if (string.IsNullOrEmpty(Config.Instance.VlcPath) || !File.Exists(Config.Instance.VlcPath))
			                                                                 	{
			                                                                 		return false;
			                                                                 	}

			                                                                 	if (this.SelectedTrailer == null || this.SelectedTrailer.FilePath.Contains("http://") == false)
			                                                                 	{
			                                                                 		return false;
			                                                                 	}
			                                                                 	else
			                                                                 	{
			                                                                 		return true;
			                                                                 	}
			                                                                 });

			this.playTrailerCommand = new RelayCommand(() =>
			                                           {
			                                           	this.CreateKernelHelper().PlayTrailer(this.SelectedTrailer.FilePath);
			                                           },
			                                           () =>
			                                           {
			                                           	if (this.SelectedTrailer == null)
			                                           	{
			                                           		return false;
			                                           	}

			                                           	if (string.IsNullOrEmpty(Config.Instance.VlcPath) || !File.Exists(Config.Instance.VlcPath))
			                                           	{
			                                           		return false;
			                                           	}

			                                           	return true;
			                                           }
			                                          );

			this.editMainDataCommand = new RelayCommand(() =>
			                                            {
			                                            	EditMainDataWindowVM vmEditMainData = new EditMainDataWindowVM(this.SelectedUiMediaItem as UI.Entity.VideoMediaElement);
			                                            	EditMainDataWindow editMainDataWindow = new EditMainDataWindow(vmEditMainData);

			                                            	editMainDataWindow.ShowDialog();
			                                            },
			                                            ()=>
			                                            {
			                                            	return this.SelectedUiMediaItem is UI.Entity.VideoMediaElement;
			                                            });

			this.addPosterCommand = new RelayCommand(()=>
			                                         {
			                                         	System.Windows.Forms.OpenFileDialog openFilePoster = new System.Windows.Forms.OpenFileDialog();
			                                         	openFilePoster.Filter = "*.jpg|*.jpg|*.png|*.png";

			                                         	if (openFilePoster.ShowDialog() == System.Windows.Forms.DialogResult.OK)
			                                         	{
			                                         		var poster = new Poster()
			                                         		{
			                                         			Image = mveEngine.ImageUtil.GetLocalImagePath(openFilePoster.FileName)
			                                         		};
			                                         		
			                                         		var video = this.SelectedUiMediaItem as UI.Entity.VideoMediaElement;
			                                         		video.PrimaryPoster = poster.ToPosterEntity();
			                                         		video.Posters.Add(video.PrimaryPoster);
			                                         		video.HasChanged = true;
			                                         		
			                                         		var mveItem = KernelSessionManager.SessionInstance.RealKernel.ItemCollection.FindById(video.Id.ToString());
			                                         		mveItem.ImagesPaths.Add(poster);
			                                         		mveItem.HasChanged = true;
			                                         	}
			                                         },
			                                         ()=>
			                                         {
			                                         	return this.SelectedUiMediaItem is UI.Entity.VideoMediaElement;
			                                         }
			                                        );

			this.clearMetadataCommand = new RelayCommand(() =>
			                                             {
			                                             	this.CreateKernelHelper().ClearMetadata(this.SelectedUiMediaItem as UI.Entity.VideoMediaElement);
			                                             },
			                                             ()=>
			                                             {
			                                             	return this.SelectedUiMediaItem is UI.Entity.VideoMediaElement;
			                                             }
			                                            );
			
			this.deselectAllBackdrops = new RelayCommand(() =>
			                                             {
			                                             	var mediaItem = SelectedUiMediaItem as UI.Entity.VideoMediaElement;
			                                             	
			                                             	foreach (var element in mediaItem.Backdrops) {
			                                             		element.Checked	= false;
			                                             	}
			                                             });

			this.removeSelectedActorCommand = new RelayCommand(() =>
			                                                   {
			                                                   	var video = this.SelectedUiMediaItem as UI.Entity.VideoMediaElement;
			                                                   	video.Actors.Remove(this.SelectedActor);
			                                                   	this.SelectedActor = null;
			                                                   	video.HasChanged = true;
			                                                   	var engineItem = KernelSessionManager.SessionInstance.RealKernel.ItemCollection.ItemsList.First(i=>i.Id == video.Id.ToString());
			                                                   	this.CreateKernelHelper().SyncItemToEngine(engineItem);
			                                                   },
			                                                   () =>
			                                                   {
			                                                   	return this.SelectedActor != null;
			                                                   });

			this.availableTrailers = new CollectionViewSource();
			this.availableTrailers.Filter += (o, args) =>
			{
				var trailer = args.Item as UI.Entity.TrailerMediaItem;

				args.Accepted = trailer.FilePath.IndexOf("http://") == 0;
			};

			this.downloadedTrailers = new CollectionViewSource();
			this.downloadedTrailers.Filter += (o, args) =>
			{
				var trailer = args.Item as UI.Entity.TrailerMediaItem;

				args.Accepted = !(trailer.FilePath.IndexOf("http://") == 0);
			};
			
			this.bannerPathHttp = new CollectionViewSource();
			
			this.bannerPathHttp.Filter += (o, args) =>
			{
				var poster = args.Item as MetaVideoEditorEvolved.UI.Entity.PosterMediaItem;
				
				args.Accepted = poster.Image.IndexOf("http://") == 0;
			};
			
			this.bannerPathLocal = new CollectionViewSource();
			
			this.bannerPathLocal.Filter += (o, args) =>
			{
				var poster = args.Item as MetaVideoEditorEvolved.UI.Entity.PosterMediaItem;
				
				args.Accepted = poster.Image.IndexOf("http://") != 0;
			};
			
			this.imagesPathHttp = new CollectionViewSource();
			
			this.imagesPathHttp.Filter += (o, args) =>
			{
				var poster = args.Item as MetaVideoEditorEvolved.UI.Entity.PosterMediaItem;
				
				args.Accepted = poster.Image.IndexOf("http://") == 0;
			};
			
			this.imagesPathLocal = new CollectionViewSource();
			
			this.imagesPathLocal.Filter += (o, args) =>
			{
				var poster = args.Item as MetaVideoEditorEvolved.UI.Entity.PosterMediaItem;
				
				args.Accepted = poster.Image.IndexOf("http://") != 0;
			};
			
			this.backdropsHttp = new CollectionViewSource();
			
			this.backdropsHttp.Filter += (o, args) =>
			{
				var poster = args.Item as MetaVideoEditorEvolved.UI.Entity.PosterMediaItem;
				
				args.Accepted = poster.Image.IndexOf("http://") == 0;
			};
			
			this.backdropsLocal = new CollectionViewSource();
			
			this.backdropsLocal.Filter += (o, args) =>
			{
				var poster = args.Item as MetaVideoEditorEvolved.UI.Entity.PosterMediaItem;
				
				args.Accepted = poster.Image.IndexOf("http://") != 0;
			};
			
			this.filterCommand = new RelayCommand<string>((param)=>
			                                              {
			                                              	var filter = new MediaFilter() {Filter = FilterType.None};
			                                              	
			                                              	if (!string.IsNullOrEmpty(param) && param=="ClearFilter") {
			                                              		this.IsFilterSerie = false;
			                                              		this.IsFilterMovie = false;
			                                              		this.IsFilterDataStateFound = false;
			                                              		this.IsFilterDataStateMiddle = false;
			                                              		this.IsFilterDataStateNotFound = false;
			                                              		this.Filter = filter;
			                                              		this.FilterTitleSearch = string.Empty;
			                                              		return;
			                                              	}
			                                              	
			                                              	if (this.IsFilterMovie || this.IsFilterSerie) {
			                                              		filter.Filter = FilterType.MediaType;
			                                              		
			                                              		if (this.IsFilterSerie) {
			                                              			filter.MediaType = MediaType.Serie;
			                                              		}
			                                              		else
			                                              		{
			                                              			filter.MediaType = MediaType.Movie;
			                                              		}
			                                              	}
			                                              	
			                                              	if (this.IsFilterDataStateFound || this.IsFilterDataStateMiddle || this.IsFilterDataStateNotFound) {
			                                              		filter.Filter = filter.Filter | FilterType.DataState;
			                                              		
			                                              		if (this.IsFilterDataStateNotFound) {
			                                              			filter.DataStateLevel = 0;
			                                              		}
			                                              		
			                                              		if (this.IsFilterDataStateMiddle) {
			                                              			filter.DataStateLevel = 1;
			                                              		}
			                                              		
			                                              		if (this.IsFilterDataStateFound) {
			                                              			filter.DataStateLevel = 3;
			                                              		}
			                                              	}
			                                              	
			                                              	if (!string.IsNullOrEmpty(this.FilterTitleSearch)) {
			                                              		filter.Filter |= FilterType.TitleSearch;
			                                              		filter.Title = this.FilterTitleSearch;
			                                              	}
			                                              	
			                                              	this.Filter = filter;
			                                              },
			                                              (param)=>
			                                              {
			                                              	if (!string.IsNullOrEmpty(param) && param=="ClearFilter") {
			                                              		if (this.Filter!=null && this.Filter.Filter!= FilterType.None) {
			                                              			return true;
			                                              		}
			                                              		else
			                                              		{
			                                              			return false;
			                                              		}
			                                              	}
			                                              	
			                                              	return true;
			                                              });
			
			this.Filter = new MediaFilter() {Filter = FilterType.None};
			
			this.clearSelectionCommand = new RelayCommand(()=>
			                                             {
			                                             	var checkedMedia = from media in KernelSessionManager.SessionInstance.KernelUiEntity.FlatListOfMediaItems.ToList()
			                                             		where media.IsCheckedForUpdate
			                                             		select media;
			                                             	
			                                             	foreach (var media in checkedMedia) {
			                                             		media.IsCheckedForUpdate = false;
			                                             	}
			                                             	
			                                             	KernelSessionManager.SessionInstance.KernelUiEntity.MediaItems[0].IsCheckedForUpdate = false;
			                                             },
			                                             ()=>
			                                             {
			                                             	return (from media in KernelSessionManager.SessionInstance.KernelUiEntity.FlatListOfMediaItems.ToList()
			                                             	        where media.IsCheckedForUpdate
			                                             	        select media).Any();
			                                             });
		}

		protected virtual Helper.IKernelHelper CreateKernelHelper()
		{
			return new Helper.KernelHelper();
		}

		protected virtual MessageBoxResult ShowQuestion(string message)
		{
			return MessageBox.Show(message, "MVE Evolved", MessageBoxButton.YesNo, MessageBoxImage.Warning);
		}

		protected virtual void ShowInfoMessage(string message)
		{
			MessageBox.Show(message, "MVE Evolved", MessageBoxButton.OK, MessageBoxImage.Information);
		}

		protected virtual void ShowWaitingWindow(Action action, string message)
		{
			var waitingWindow = new WaitingWindow(action, message);
			waitingWindow.ShowDialog();
		}

		protected virtual void ShowErrorMessage(string message)
		{
			MessageBox.Show(message, "MVE Evolved", MessageBoxButton.OK, MessageBoxImage.Error);
		}

		protected virtual Data.IFileManager CreateFileManager()
		{
			return new Data.FileManager();
		}

		private void SaveAll()
		{
			var items = from media in this.UiKernel.FlatListOfMediaItems
				where media is UI.Entity.VideoMediaElement && ((UI.Entity.VideoMediaElement) media).HasChanged
				select media;

			new MultiAutoSearch()
			{
				DataContext = new MultiAutoSearchVM(items.ToList(), true)
			}.ShowDialog();
		}

		private void RebuildLibrary(bool isFromSettings, bool canRebuild)
		{
			bool rebuild = false;

			if (!isFromSettings)
			{
				if (this.ShowQuestion(this.CreateKernelHelper().GetString("DeleteCacheMess")) == MessageBoxResult.Yes)
				{
					rebuild = true;
				}
			}
			else
			{
				rebuild = canRebuild;
			}


			if (rebuild)
			{
				this.SelectedUiMediaItem = null;
				this.UiKernel.FlatListOfMediaItems.Clear();
				this.UiKernel.MediaItems.Clear();

				var window = new SplahScreen(true);
				window.ShowDialog();
			}
		}

		private void SaveSelectedItem()
		{
			// Validate item before save
			var video = this.SelectedUiMediaItem as UI.Entity.VideoMediaElement;
			var message = this.CreateKernelHelper().ValidateItem(video);
			if (!string.IsNullOrEmpty(message))
			{
				this.ShowErrorMessage(message);
			}
			else
			{
				var mveItem = KernelSessionManager.SessionInstance.RealKernel.ItemCollection.ItemsList.First(i=>i.Id == video.Id.ToString());
				var changedPoster = from p in video.Posters
					where p.IsDirty select p;
				
				foreach (var poster in changedPoster) {
					mveItem.ImagesPaths.First(p=>p.Image == poster.Image).Checked = poster.Checked;
				}
				
				video.IsInError = false;
				mveItem = KernelSessionManager.SessionInstance.RealKernel.ItemCollection.ItemsList.First(i=>i.Id == video.Id.ToString());
				mveItem.IsInError = false;
				
				this.CreateKernelHelper().SaveItem(video);
				mveItem = KernelSessionManager.SessionInstance.RealKernel.ItemCollection.ItemsList.First(i=>i.Id == video.Id.ToString());
				mveItem.CopyToMediaUiEntity(video);

				this.RefreshCollectionViewSource();
			}
		}
		
		private void RefreshCollectionViewSource()
		{
			if (this.ImagesPathHttp.View!=null) {
				this.ImagesPathHttp.View.Refresh();
			}
			
			if (this.ImagesPathLocal.View!=null) {
				this.ImagesPathLocal.View.Refresh();
			}
			
			if (this.BackdropsLocal.View!=null) {
				this.BackdropsLocal.View.Refresh();
			}
			
			if (this.BannerPathLocal.View!=null) {
				this.BannerPathLocal.View.Refresh();
			}
			
			if (this.BackdropsHttp.View!=null) {
				this.BackdropsHttp.View.Refresh();
			}
			
			if (this.BannerPathHttp.View!=null) {
				this.BannerPathHttp.View.Refresh();
			}
		}

		private void AutoSearchClicked()
		{
			var selectedItem = from media in this.UiKernel.FlatListOfMediaItems.ToList()
				where media.IsCheckedForUpdate select media;
			

			if (selectedItem.Count() == 1 && selectedItem.First() is UI.Entity.VideoMediaElement)
			{
				var video = selectedItem.First() as UI.Entity.VideoMediaElement;
				var mediaItem = selectedItem.First();
				Action search = new Action(() =>
				                           {
				                           	try
				                           	{
				                           	var updated = this.CreateKernelHelper().AutoSearchMediaItem(video);
				                           	this.CreateKernelHelper().SyncItemToEngine(updated);
				                           	video.DataState = updated.DataState;

				                           	// Item is changed in kernel, need to reflect into entity collection and on screen
				                           	// Launch property changed to force refresh on screen
				                           	GalaSoft.MvvmLight.Threading.DispatcherHelper.CheckBeginInvokeOnUI(new Action(() =>
				                           	                                                                              {
				                           	                                                                              	updated.CopyToMediaUiEntity(mediaItem);
				                           	                                                                              }));
				                           	}
				                           	catch(MetaVideoEditorEvolved.AddIn.Entity.InvalidPluginException ex)
				                           	{
				                           		GalaSoft.MvvmLight.Threading.DispatcherHelper.UIDispatcher.Invoke(new Action(() =>
				                           	                                                                              {
				                           		                                                                              	MessageBox.Show(ex.Message,"MVE", MessageBoxButton.OK,MessageBoxImage.Error);
				                           		                                                                              }));
				                           		                                                                              
				                           	}
				                           	catch(Exception)
				                           	{
				                           		GalaSoft.MvvmLight.Threading.DispatcherHelper.UIDispatcher.Invoke(new Action(() =>
				                           	                                                                              {
				                           		                                                                              	MessageBox.Show(MetaVideoEditorEvolved.Properties.Resources.Global_Messages_UnkownSearchError,"MVE", MessageBoxButton.OK,MessageBoxImage.Error);
				                           		                                                                              }));
				                           		                                                                              
				                           	}
				                           });

				this.ShowWaitingWindow(search, "Recherche des données...");

				if (!video.HasChanged)
				{
					if (this.ShowQuestion(this.CreateKernelHelper().GetString("SearchManuallyMessage")) == MessageBoxResult.Yes)
					{
						new Search()
						{
							DataContext = new SearchVM(video)
						}.ShowDialog();
					}
				}
			}
			else
			{
				new MultiAutoSearch()
				{
					DataContext = new MultiAutoSearchVM(selectedItem.Reverse().ToList(), false)
				}.ShowDialog();
			}
			
			var videoUpdated = this.SelectedUiMediaItem as UI.Entity.VideoMediaElement;
			if(videoUpdated!=null && videoUpdated.HasChanged && videoUpdated.Backdrops.Any())
			{
				this.BackdropsHttp.Source = videoUpdated.Backdrops;
				this.backdropsHttp.View.Refresh();
				
				this.BackdropsLocal.Source = videoUpdated.Backdrops;
				this.BackdropsLocal.View.Refresh();
			}
			
			var serie = videoUpdated as UI.Entity.SerieMediaItem;
			if (serie!=null && serie.HasChanged && serie.Banners.Any()) {
				this.BannerPathHttp.Source = serie.Banners;
				this.BannerPathHttp.View.Refresh();
				
				this.BannerPathLocal.Source = serie.Banners;
				this.BannerPathLocal.View.Refresh();
			}
		}

		private void ChangeActionStatus(string message, ActionStatus status)
		{
			if (this.dispatcher.Dispatcher.Thread != Thread.CurrentThread)
			{
				this.dispatcher.Dispatcher.Invoke(new Action(() => this.ChangeActionStatus(message, status)));
				return;
			}

			lock (this.Actions)
			{
				var action = (from a in this.Actions
				              where a.Message == message && a.Status != status
				              select a).FirstOrDefault();

				if (action != null)
				{
					action.Status = status;
				}
			}
		}

		private void Loaded()
		{
			
		}
		
		private bool RemoveSelectedItem()
		{
			this.UiKernel.FlatListOfMediaItems.Remove(this.SelectedUiMediaItem);
			return UI.Helper.ConverterHelper.RemoveMediaFromTree(this.UiKernel.MediaItems, this.SelectedUiMediaItem, null);
		}
	}
}
